home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gnulib / libsrc98.zoo / isatty.c < prev    next >
C/C++ Source or Header  |  1993-03-01  |  1KB  |  52 lines

  1. /* from the original GCC TOS library by jrd */
  2. /* this algorithm is due to Allan Pratt @ Atari.  Thanks Allan! */
  3.  
  4. #include <errno.h>
  5. #include <osbind.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <device.h>
  10. #include "lib.h"
  11.  
  12. int isatty(fd)
  13. int fd;
  14. {
  15.   int rc;
  16.   long oldloc, seekval;
  17.   int handle = __OPEN_INDEX(fd);
  18.   struct _device *dev;
  19.  
  20.   if ((short)handle < 0)
  21.     return(1);
  22.  
  23.   dev = _dev_fd(fd);
  24.   if (dev && ((dev->open) || (dev->dev == 0xfffd)))
  25.   {    /* a special device or PRN: */
  26.       return 0;
  27.   }
  28.   
  29.   if (handle < __NHANDLES)
  30.     if (__open_stat[handle].status != FH_UNKNOWN)
  31.         return(__open_stat[handle].status == FH_ISATTY);
  32.   oldloc = Fseek (0L, fd, SEEK_CUR);    /* seek zero bytes from current loc */
  33.   if ((seekval = Fseek (1L, fd, SEEK_SET)) != 0) /* try to seek ahead one byte */
  34.     if ((seekval > 0) || (seekval == ((long)(-EBADARG)))) /* out of range... */
  35.       rc = 0;            /* file, not a tty */
  36.     else 
  37.       {
  38.       errno = EBADF;        /* any other error returns "invalid handle" */
  39.                 /* because you can't tell */
  40.       rc = 0;
  41.       }
  42.   else
  43.     rc = 1;            /* yes, tty (Fseek returns 0 only for ttys) */
  44.   (void)Fseek(oldloc, fd, SEEK_SET);    /* seek back to original location */
  45.   if (handle < __NHANDLES)
  46.     if (rc)
  47.         __open_stat[handle].status = FH_ISATTY;
  48.         else
  49.         __open_stat[handle].status = FH_ISAFILE;
  50.   return (rc);            /* return true, false, or error */
  51. }
  52.